using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using MauiBuilder.Services;
using MauiBuilder.Views;
using Font = Microsoft.Maui.Font;

namespace MauiBuilder
{
    /// <summary>
    /// 应用程序Shell类，负责应用程序的导航和路由
    /// </summary>
    public partial class AppShell : Shell
    {
        /// <summary>
        /// 构造函数，初始化Shell并注册路由
        /// </summary>
        public AppShell()
        {
            InitializeComponent();
            
            // 注册路由
            RegisterRoutes();
            
            LogManager.Instance.LogInfo("AppShell初始化完成");
        }
        
        /// <summary>
        /// 注册应用程序路由
        /// </summary>
        private void RegisterRoutes()
        {
            LogManager.Instance.LogInfo("开始注册应用程序路由");
            
            Routing.RegisterRoute("LoginPage", typeof(LoginPage));
            Routing.RegisterRoute("RegisterPage", typeof(RegisterPage));
            Routing.RegisterRoute("SettingsPage", typeof(SettingsPage));
            Routing.RegisterRoute("AboutPage", typeof(AboutPage));
            Routing.RegisterRoute("DeviceInfoPage", typeof(DeviceInfoPage));
            Routing.RegisterRoute("HybridWebPage", typeof(HybridWebPage));
            Routing.RegisterRoute("PaymentPage", typeof(PaymentPage));
            Routing.RegisterRoute("ChatPage", typeof(ChatPage));
            Routing.RegisterRoute("SpeechToTextPage", typeof(SpeechToTextPage));
            
            LogManager.Instance.LogInfo("路由注册完成");
        }

        
        /// <summary>
        /// 显示Snackbar消息
        /// </summary>
        /// <param name="message">要显示的消息</param>
        /// <returns>异步任务</returns>
        public static async Task DisplaySnackbarAsync(string message)
        {
            LogManager.Instance.LogInfo($"显示Snackbar消息: {message}");
            
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

            var snackbarOptions = new SnackbarOptions
            {
                BackgroundColor = Color.FromArgb("#FF3300"),
                TextColor = Colors.White,
                ActionButtonTextColor = Colors.Yellow,
                CornerRadius = new CornerRadius(0),
                Font = Font.SystemFontOfSize(18),
                ActionButtonFont = Font.SystemFontOfSize(14)
            };

            var snackbar = Snackbar.Make(message, visualOptions: snackbarOptions);

            await snackbar.Show(cancellationTokenSource.Token);
        }

        /// <summary>
        /// 显示Toast消息
        /// </summary>
        /// <param name="message">要显示的消息</param>
        /// <returns>异步任务</returns>
        public static async Task DisplayToastAsync(string message)
        {
            LogManager.Instance.LogInfo($"尝试显示Toast消息: {message}");
            
            // Toast is currently not working in MCT on Windows
            if (OperatingSystem.IsWindows())
            {
                LogManager.Instance.LogWarning("Windows平台不支持Toast消息，已跳过显示");
                return;
            }

            var toast = Toast.Make(message, textSize: 18);

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            await toast.Show(cts.Token);
            
            LogManager.Instance.LogInfo("Toast消息显示完成");
        }
        
        /// <summary>
        /// 统一的消息提示方法
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">消息内容</param>
        /// <param name="cancel">确认按钮文本，默认为"确定"</param>
        /// <returns>异步任务</returns>
        public static async Task DisplayAlertAsync(string title, string message, string cancel = "确定")
        {
            LogManager.Instance.LogInfo($"显示Alert消息 - 标题: {title}, 内容: {message}");
            
            try
            {
                var shell = Shell.Current;
                if (shell != null)
                {
                    await shell.DisplayAlert(title, message, cancel);
                }
                else
                {
                    LogManager.Instance.LogWarning("Shell为空，无法显示Alert消息");
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError($"显示Alert消息失败: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 统一的确认对话框方法
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">消息内容</param>
        /// <param name="accept">确认按钮文本，默认为"确定"</param>
        /// <param name="cancel">取消按钮文本，默认为"取消"</param>
        /// <returns>用户选择结果</returns>
        public static async Task<bool> DisplayConfirmAsync(string title, string message, string accept = "确定", string cancel = "取消")
        {
            LogManager.Instance.LogInfo($"显示确认对话框 - 标题: {title}, 内容: {message}");
            
            try
            {
                var shell = Shell.Current;
                if (shell != null)
                {
                    var result = await shell.DisplayAlert(title, message, accept, cancel);
                    LogManager.Instance.LogInfo($"确认对话框结果: {result}");
                    return result;
                }
                else
                {
                    LogManager.Instance.LogWarning("Shell为空，无法显示确认对话框");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError($"显示确认对话框失败: {ex.Message}");
                return false;
            }
        }
    }
}
